import { eventFormat, stateFormat } from "..";
import { bindEvent } from "../compiler/event";

export function useDOM(
  { template, state, methods },
  rootDOM
) {
  // console.log(template, state, methods, rootDOM)

  rootDOM.innerHTML = render(template, state);
  bindEvent(methods);
}

export function render(template, state) {
  // 事件处理 数据替换

  template = eventFormat(template, state);
  template = stateFormat(template, state);

  return template;
}

export function update(statePool, key, value) {
  const allElement = document.querySelectorAll('*');
  let oItem = null;

  statePool.forEach(item => {
    if (item.state[item.state.length - 1] === key) {
      for (let i = 0; i < allElement.length; i++) {
        oItem = allElement[i];

        const _mark = parseInt(oItem.dataset.mark);
        if (item.mark === _mark) {
          oItem.innerHTML = value;
        }
      }
    }
  })
  console.log()
}